﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProsonUser.DomainUsers;
using ProsonUser.IPrisinCourses;
using ProsonUser.IPrisinCoursesDto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ProsonUser.ProsonCourses
{
   public class Coursese : ApplicationService, ICourses
    {
        private readonly IRepository<Courses, int> _courses;
        public Coursese(IRepository<Courses, int> courses)
        {
            _courses = courses;
        }
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file">来自form表单的文件信息</param>
        /// <returns></returns>
        [HttpPost]
        public string UploadFile(IFormFile file)
        {
            string rootdir = AppContext.BaseDirectory.Split(@"\bin\")[0];
            string fname = DateTime.Now.ToString("yyyyMMddHHmmssffff") + System.IO.Path.GetExtension(file.FileName);
            var path = rootdir + @"\Upload\" + fname;
            using (System.IO.FileStream fs = System.IO.File.Create(path))
            {
                file.CopyTo(fs);
                fs.Flush();//清空文件流
            }
            return "http://localhost:8023/" + fname;//将能访问新文件的网址回传给前端
        }
        [HttpPost]
        public async Task<int> CoursesAdd(CreateUpdateCoursesDto coursesDto)
        {
            var lst = await _courses.InsertAsync(ObjectMapper.Map<CreateUpdateCoursesDto, Courses>(coursesDto));
            if (lst != null)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        [HttpGet]
        public List<CoursesDto> CoursesList()
        {
            var list = _courses.ToList();
            return ObjectMapper.Map<List<Courses>, List<CoursesDto>>(list);
        }
    }
}
